﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.Collections.ObjectModel;
using Coding4Fun.Phone.Controls;
using Coding4Fun.Phone.Site.Controls;
using Microsoft.Phone.UserData;
using Microsoft.Phone.Scheduler;
using Microsoft.Phone.Shell;
using System.IO;
using System.IO.IsolatedStorage;


namespace wp7_upmc
{
    public partial class MainPage : PhoneApplicationPage
    {
        List<GiftItem> lstGiftItems;

        String twitterName = "";

        // Constructor
        public MainPage()
        {
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(MainPage_Loaded);
        }

        void ShowNotifyForTwitterName()
        {
            var input = new InputPrompt
            {
                Title = "Gift Idea",
                Message = "Please provide your twitter username, Gift Idea will soon support more services as Facebook etc.",
            };
            input.Completed += new EventHandler<PopUpEventArgs<string, PopUpResult>>(input_Completed);

            input.Show();
        }

        void input_Completed(object sender, PopUpEventArgs<string, PopUpResult> e)
        {
            if (e.PopUpResult == PopUpResult.Ok)
            {
                if (e.Result.Length < 2)
                    return;

                IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;
                settings["twitterName"] = e.Result;
                twitterName = e.Result;
                LoadTwitter();
            }
        }

        void LoadTwitter()
        {
            progressUp.Visibility = System.Windows.Visibility.Visible;
            TwitterManager tw = new TwitterManager();
            tw.GetUserTimeline(twitterName, lst => {
                lstProducs.Dispatcher.BeginInvoke(delegate()
                {
                    lstProducs.ItemsSource = lst;
                    progressUp.Visibility = System.Windows.Visibility.Collapsed;
                });
            });
        }


        void MainPage_Loaded(object sender, RoutedEventArgs e)
        {
            if(!IsolatedStorageSettings.ApplicationSettings.TryGetValue("twitterName", out twitterName))
                ShowNotifyForTwitterName();
            else
                LoadTwitter();

            trexStoryboard.Begin();

            
            //FillData();

            //lstProducs.ItemsSource = lstGiftItems;

            lstContacts.IsEnabled = false;

     Contacts objContacts = new Contacts();
     objContacts.SearchCompleted += new EventHandler<ContactsSearchEventArgs>(objContacts_SearchCompleted);
     //objContacts.SearchAsync(string.Empty, FilterKind.None, null);
}

void objContacts_SearchCompleted(object sender, ContactsSearchEventArgs e)
{

   var ContactsData = from m in e.Results
                      select new MyContacts
                      {
                          DisplayName = m.DisplayName,
                          PhoneNumber = m.PhoneNumbers.FirstOrDefault()

                       };
   lstContacts.ItemsSource = ContactsData;

   }

        private void FillData()
        {
            lstGiftItems = new List<GiftItem>();
            for (int i = 0; i < 10; i++)
			{
                lstGiftItems.Add(new GiftItem{
                                 ItemTitle = "Gift " + i,
                                 ItemDescription = "Item Description goes here " + i,
                                 ItemPrice = (3 * i).ToString(),
                                 ItemTitle2 = "title2"
                                }); 
			}
            
        }

        private void Tile_Click(object sender, RoutedEventArgs e)
        {
            ;
        }

        private void ShowGifts_Click(object sender, RoutedEventArgs e)
        {
            progressbar.Visibility = System.Windows.Visibility.Visible;
            progressbar.IsIndeterminate = true;
        }

        private void About_Click(object sender, EventArgs e)
        {
            var about = new AboutPrompt();
            about.Show("UPMC Students", "martani_net", "martani.net@gmail.com", "http://martani.net");
        }

        bool allContactsChoosed = true;

        private void AllContacts_Click(object sender, RoutedEventArgs e)
        {
            if (allContactsChoosed)
            {
                btnAllContactsToggle.Content = "Choose individual contacts";
            }
            else
                btnAllContactsToggle.Content = "All contacts";

            lstContacts.IsEnabled = !lstContacts.IsEnabled;
            allContactsChoosed = !allContactsChoosed;
        }

        private void btnAddReminder_Click(object sender, RoutedEventArgs e)
        {
            NavigationService.Navigate(new Uri("/AddReminder.xaml", UriKind.Relative));
        }


        IEnumerable<ScheduledNotification> notifications;
        private void ResetItemsList()
        {
            // Use GetActions to retrieve all of the scheduled actions
            // stored for this application. The type <Reminder> is specified
            // to retrieve only Reminder objects.
            //reminders = ScheduledActionService.GetActions<Reminder>();
            notifications = ScheduledActionService.GetActions<ScheduledNotification>();

            // If there are 1 or more reminders, hide the "no reminders"
            // TextBlock. IF there are zero reminders, show the TextBlock.
            //if (reminders.Count<Reminder>() > 0)
            /*if (notifications.Count<ScheduledNotification>() > 0)
            {
                EmptyTextBlock.Visibility = Visibility.Collapsed;
            }
            else
            {
                EmptyTextBlock.Visibility = Visibility.Visible;
            }*/

            // Update the ReminderListBox with the list of reminders.
            // A full MVVM implementation can automate this step.
            NotificationListBox.ItemsSource = notifications;

            ShellTile TileToFind = ShellTile.ActiveTiles.First();
            StandardTileData NewTileData = new StandardTileData()
                                                {
                                                    Count = notifications.ToList().Count
                                                };

            TileToFind.Update(NewTileData);
        }

        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            //Reset the ReminderListBox items when the page is navigated to.
            ResetItemsList();
        }

        private void deleteButton_Click(object sender, RoutedEventArgs e)
        {
            // The scheduled action name is stored in the Tag property
            // of the delete button for each reminder.
            string name = (string)((Button)sender).Tag;

            // Call Remove to unregister the scheduled action with the service.
            ScheduledActionService.Remove(name);

            // Reset the ReminderListBox items
            ResetItemsList();
        }
    }
}

public class GiftItem
{
    public string ItemTitle { get; set; }
    public string ItemDescription { get; set; }
    public string ItemTitle2 { get; set; }
    public string ItemPrice { get; set; }
    public string ItemImageUrl { get; set; }
}

public class MyContacts
{

   public string DisplayName { get; set; }
   public ContactPhoneNumber PhoneNumber { get; set; }

}